/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.rooms.banner.sh3d;

import java.awt.Color;
import java.util.StringTokenizer;

import com.datasentinel.rooms.file.sh3d.HaiVanPassPictureFrameFurnitureSupport;
import com.eteks.sweethome3d.tools.ResourceURLContent;

public class BannerPieceFurnitureSupport extends HaiVanPassPictureFrameFurnitureSupport
{
	private static final String BODY = "<body";
	private static BannerPieceFurnitureSupport instance;
	
	public static BannerPieceFurnitureSupport getInstance()
	{
		if (instance == null)
			instance = new BannerPieceFurnitureSupport();
		return instance;
	}

	protected BannerPieceFurnitureSupport()
	{
		super(new ResourceURLContent(BannerStringPieceOfFurniture.class, "square.obj"));
	}
		
	@Override
	protected String getShapeName()
	{
		return "front.jpg";
	}

	public static Color getBackgroundFromHTML(String html) throws NumberFormatException
	{
		Color color = null;
		int indexOfBody = html.indexOf(BODY);
		if (indexOfBody > 0)
		{
			int transparency = 255;
			indexOfBody += BODY.length();
			int indexOfBracket = html.indexOf(">", indexOfBody);
			StringTokenizer st = new StringTokenizer(html.substring(indexOfBody, indexOfBracket), " =\"", true);
			while (st.hasMoreTokens())
			{
				String parameter;
				do
				{
					parameter = st.nextToken();
				} while (parameter.equals(" ") && st.hasMoreTokens());
				
				String value = "";
				boolean foundEquals = false;
				boolean needParenthesis = false;
outer:			while (st.hasMoreTokens())
				{
					String text = st.nextToken();
					if (text.length() == 1)
						switch (text.charAt(0))
						{
							case ' ':	
								if (needParenthesis)
									value += text;	// embedded space in argument
								else if (value.length() > 0) 
									break outer; 	// argument complete
								break;
							case '=':	
								foundEquals = true;		
								break;
							case '"':	
								if (needParenthesis) 
									break outer; 	// argument complete
								else 
									needParenthesis = true;
								break;
							default: 
								value += text;
								break;
						}
					else
						value += text;
					
				}
				if (foundEquals)
					if (parameter.equalsIgnoreCase("color"))
					{
						if (value.startsWith("#"))
							value = value.substring(1);
						color = new Color((int)Long.parseLong(value, 16) & 0xffffff);
					}
					else if (parameter.equalsIgnoreCase("transparency"))
						transparency = Integer.parseInt(value);
			}
			if (color != null && transparency < 255)
				color = new Color(color.getRed(), color.getGreen(), color.getBlue(), transparency);
		}	
		return color;
	}
}
